package com.guawazi.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.guawazi.MainActivity;
import com.guawazi.R;
import com.guawazi.activity.MapActivity;
import com.guawazi.bean.DataEntity;
import com.guawazi.utils.Utils;
import com.guawazi.widget.scrollablepanel.PanelAdapter;

import java.util.ArrayList;
import java.util.List;

public class ScrollablePanelAdapter extends PanelAdapter {
    private Context context;
    private static final int TITLE_TYPE = 4;
    private static final int MAP_TYPE = 0;
    private static final int ATTR_TYPE = 1;
    private static final int CONTENT_TYPE = 2;

    private List<DataEntity> dateInfoList = new ArrayList<>();
    private List<String> attrList = new ArrayList<>();

    public ScrollablePanelAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getRowCount() {
        return dateInfoList.size() + 1;
    }

    @Override
    public int getColumnCount() {
        return attrList.size() + 1;
    }


    //绑定数据的回调
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int row, int column) {
        int viewType = getItemViewType(row, column);
        switch (viewType) {
            case ATTR_TYPE:
                setAttrView(column, (AttrViewHolder) holder);
                break;
            case MAP_TYPE:
                setMapView(row, (MapViewHolder) holder);
                break;
            case CONTENT_TYPE:
                setContentView(row, column, (ContentViewHolder) holder);
                break;
            case TITLE_TYPE:
                break;
            default:
                setContentView(row, column, (ContentViewHolder) holder);
        }
    }

    public void setAttrView(int pos, AttrViewHolder holder) {
        holder.titleTv.setText(attrList.get(pos - 1));
    }

    public void setMapView(final int pos, MapViewHolder holder) {
        holder.mapIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String longitude = dateInfoList.get(pos - 1).getLongitude();
                String latitude = dateInfoList.get(pos - 1).getLatitude();

                Bundle bundle = new Bundle();
                bundle.putString("latitude", Utils.transformLocation(latitude));
                bundle.putString("longitude", Utils.transformLocation(longitude));
                ((MainActivity) context).readyGo(MapActivity.class, bundle);
            }
        });
    }

    public void setContentView(int row, int colum, ContentViewHolder holder) {
        DataEntity entity = dateInfoList.get(row - 1);
        int indexColum = colum - 1;
        switch (indexColum) {
            case 0:
                holder.contentTv.setText(entity.getVoltageValue() + "");
                break;
            case 1:
                holder.contentTv.setText(entity.getMarkerPosition());
                break;
            case 2:
                holder.contentTv.setText(entity.getAccelerationX() + "");
                break;
            case 3:
                holder.contentTv.setText(entity.getAccelerationY() + "");
                break;
            case 4:
                holder.contentTv.setText(entity.getAccelerationY() + "");
                break;
            case 5:
                holder.contentTv.setText(entity.getTime() + "");//TODO
                break;
            case 6:
                holder.contentTv.setText(Utils.adddufenmiao(entity.getLatitude()));
                break;
            case 7:
                holder.contentTv.setText(Utils.adddufenmiao(entity.getLongitude()));
                break;
            case 8:
                holder.contentTv.setText(Utils.transformDate(entity.getDate() + ""));
                break;
            case 9:
                holder.contentTv.setText(entity.getJudgmentPosition());
                break;
        }

    }

    public int getItemViewType(int row, int column) {
        if (column == 0 && row == 0) {
            return TITLE_TYPE;
        }
        if (column == 0) {
            return MAP_TYPE;
        }
        if (row == 0) {
            return ATTR_TYPE;
        }
        return CONTENT_TYPE;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case ATTR_TYPE:
                return new AttrViewHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.listitem_attr_info, parent, false));
            case MAP_TYPE:
                return new MapViewHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.listitem_map_info, parent, false));
            case CONTENT_TYPE:
                return new ContentViewHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.listitem_content_info, parent, false));
            case TITLE_TYPE:
                return new TitleViewHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.listitem_title, parent, false));
            default:
                break;
        }
        return new ContentViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.listitem_content_info, parent, false));
    }


    private static class AttrViewHolder extends RecyclerView.ViewHolder {
        public TextView titleTv;

        public AttrViewHolder(View itemView) {
            super(itemView);
            this.titleTv = (TextView) itemView.findViewById(R.id.title_tv);
        }
    }

    private static class MapViewHolder extends RecyclerView.ViewHolder {
        public ImageView mapIv;

        public MapViewHolder(View itemView) {
            super(itemView);
            this.mapIv = itemView.findViewById(R.id.map_iv);
        }
    }


    private static class ContentViewHolder extends RecyclerView.ViewHolder {
        public TextView contentTv;

        public ContentViewHolder(View view) {
            super(view);
            this.contentTv = (TextView) view.findViewById(R.id.content_tv);
        }
    }

    private static class TitleViewHolder extends RecyclerView.ViewHolder {
        public TextView titleTextView;

        public TitleViewHolder(View view) {
            super(view);
            this.titleTextView = (TextView) view.findViewById(R.id.title);
        }
    }


    public void setDataInfoList(List<DataEntity> list) {
        this.dateInfoList = list;
    }

    public void setAttrInfoList(List<String> list) {
        this.attrList = list;
    }

}
